<template>
  <div class="jsbos-common-layout">
    <div class="jsbos-common-layout-center">
      <el-row class="jsbos-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="6">
            <el-form-item :label="$t('common.keyword')">
              <el-input
                v-model="listQuery.keyword"
                placeholder="名称"
                clearable
                @keyup.enter.native="search()"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="试卷目录">
              <TreeSelect
                v-model="query.categoryId"
                placeholder="请选择"
                :options="categoryOptions"
                :props="categoryProps"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="试卷类型">
              <el-select
                v-model="query.paperType"
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="(item, index) in paperTypeOptions"
                  :key="index"
                  :label="item.fullName"
                  :value="item.id"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()"
                >查询</el-button
              >
              <el-button icon="el-icon-refresh-right" @click="reset()"
                >重置</el-button
              >
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="jsbos-common-layout-main jsbos-flex-main">
        <div class="jsbos-common-head">
          <div>
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click="addOrUpdateHandle('1')"
              >固定试卷
            </el-button>
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click="addOrUpdateHandle('2')"
              >随机试卷
            </el-button>
          </div>
          <div class="jsbos-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link
                icon="icon-ym icon-ym-Refresh JSBOS-common-head-icon"
                :underline="false"
                @click="reset()"
              />
            </el-tooltip>
            <screen-full isContainer />
            <el-tooltip effect="dark" content="列设置" placement="top">
              <el-link
                icon="icon-ym icon-ym-options jsbos-common-head-icon"
                :underline="false"
                @click="() => this.$refs.jsbosTableRef.showDrawer()"
              />
            </el-tooltip>
          </div>
        </div>
        <jsbos-table
          v-loading="listLoading"
          :data="list"
          ref="jsbosTableRef"
          @sort-change="sortChange"
        >
          <el-table-column
            prop="fullName"
            label="试卷名称"
            width="0"
            :align="'left'"
          />
          <el-table-column
            label="试卷目录"
            width="150"
            prop="categoryId"
            algin="left"
          >
            <template slot-scope="scope">
              {{ scope.row.categoryId | dynamicTreeText(categoryOptions) }}
            </template>
          </el-table-column>
          <el-table-column
            label="试卷类型"
            width="100"
            prop="paperType"
            algin="left"
          >
            <template slot-scope="scope">
              {{ scope.row.paperType | dynamicText(paperTypeOptions) }}
            </template>
          </el-table-column>
          <el-table-column
            prop="passScore"
            label="及格分"
            width="80"
            align="center"
          />
          <el-table-column
            prop="myScore"
            label="总分"
            width="50"
            align="center"
          />
          <el-table-column
            prop="questionNum"
            label="题数"
            width="50"
            align="center"
          />
          <el-table-column
            prop="lastModifyUserId"
            label="修改用户"
            width="100"
            align="center"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="lastModifyTime"
            label="修改时间"
            width="150"
            align="center"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="操作" width="100">
            <template slot-scope="scope">
              <el-button
                type="text"
                @click="addOrUpdateHandle(scope.row.paperType, scope.row.id)"
                >编辑
              </el-button>
              <el-button
                type="text"
                class="jsbos-table-delBtn"
                @click="handleDel(scope.row.id)"
                >删除
              </el-button>
            </template>
          </el-table-column>
        </jsbos-table>
        <pagination
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          @pagination="initData"
        />
      </div>
    </div>
    <JSBOS-Form
      v-if="formVisible"
      ref="JSBOSForm"
      @refresh="refresh"
      :categoryIdOptions="categoryOptions"
      :categoryIdProps="categoryProps"
    />
    <Random-Form
      v-if="randomFormVisible"
      ref="RandomFormRef"
      @refresh="refresh"
      :categoryIdOptions="categoryOptions"
      :categoryIdProps="categoryProps"
    />
    <ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />
    <Detail
      v-if="detailVisible"
      ref="Detail"
      @refresh="detailVisible = false"
      :categoryIdOptions="categoryOptions"
      :categoryIdProps="categoryProps"
    />
  </div>
</template>

<script>
import { getSelector, getCategorySelector } from "@/views/exam/api/category";
import { getList, Delete } from "@/views/exam/api/paper";
import request from "@/utils/request";
import JSBOSForm from "./Form";
import RandomForm from "./FormRandom";
import ExportBox from "./ExportBox";
import Detail from "./Detail";

export default {
  name: "paper-list",
  components: { JSBOSForm, RandomForm, ExportBox, Detail },
  data() {
    return {
      folderType: "paper", // 目录类型：question题目目录;paper试卷目录;exam考试目录
      isAdministrator: false,
      companyId: "",
      detailVisible: false,
      query: {
        categoryId: undefined,
        fullName: undefined,
        enCode: undefined,
        paperType: undefined,
      },
      list: [],
      listLoading: true,
      total: 0,
      listQuery: {
        keyword: "",
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "",
      },
      formVisible: false,
      randomFormVisible: false,
      exportBoxVisible: false,
      columnList: [
        { prop: "categoryId", label: "试卷目录id" },
        { prop: "enCode", label: "编码" },
        { prop: "fullName", label: "名称" },
        { prop: "intro", label: "试卷介绍" },
        { prop: "paperType", label: "试卷类型" },
        { prop: "paperrandomVersion", label: "随机试卷" },
        { prop: "openDifficultyGrade", label: "随机试卷" },
        { prop: "folders", label: "随机试题" },
        { prop: "passLine", label: "及格线(%)" },
        { prop: "description", label: "描述" },
        { prop: "enabledMark", label: "有效标志" },
        { prop: "creatorUserId", label: "创建用户" },
        { prop: "creatorTime", label: "创建时间" },
      ],
      categoryOptions: [],
      categoryProps: {
        children: "children",
        label: "fullName",
        value: "id",
      },
      paperTypeOptions: [
        { fullName: "固定试卷", id: "1" },
        { fullName: "随机试卷", id: "2" },
      ],
      paperTypeProps: {
        label: "fullName",
        value: "id",
      },
      paperrandomVersionOptions: [
        { fullName: "固定版本", id: "1" },
        { fullName: "随机版本", id: "2" },
      ],
      paperrandomVersionProps: {
        label: "fullName",
        value: "id",
      },
    };
  },
  computed: {
    menuId() {
      return this.$route.meta.modelId || "";
    },
  },
  created() {
    this.isAdministrator = this.$store.getters.userInfo.isAdministrator;
    this.companyId = this.$store.getters.userInfo.organizeId;
    this.query.companyId = this.companyId;
    this.initData();
    this.getCategoryOptions();
  },
  methods: {
    getCategoryOptions() {
      // 获取所属目录
      getCategorySelector({
        folderType: this.folderType,
        parentId: "-1",
        menuId: this.menuId,
      }).then((res) => {
        this.categoryOptions = res.data;
      });
    },
    goDetail(id) {
      this.detailVisible = true;
      this.$nextTick(() => {
        this.$refs.Detail.init(id);
      });
    },
    sortChange({ column, prop, order }) {
      this.listQuery.sort = order == "ascending" ? "asc" : "desc";
      this.listQuery.sidx = !order ? "" : prop;
      this.initData();
    },
    initData() {
      this.listLoading = true;
      let _query = {
        ...this.listQuery,
        ...this.query,
        menuId: this.menuId,
      };
      getList(_query).then((res) => {
        console.log("---res", res);
        // var _list = [];
        // for (let i = 0; i < res.data.list.length; i++) {
        //   let _data = res.data.list[i];
        //   _list.push(_data);
        // }
        this.list = res.data.list;
        this.total = res.data.pagination.total;

        this.listLoading = false;
      });
    },
    handleDel(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          Delete(id).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
    addOrUpdateHandle(paperType, id) {
      if (paperType && paperType == "2") {
        this.randomFormVisible = true;
        this.$nextTick(() => {
          this.$refs.RandomFormRef.init(id);
        });
      } else {
        this.formVisible = true;
        this.$nextTick(() => {
          this.$refs.JSBOSForm.init(id);
        });
      }
    },
    exportData() {
      this.exportBoxVisible = true;
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList);
      });
    },
    download(data) {
      let query = {
        ...data,
        ...this.listQuery,
        ...this.query,
        menuId: this.menuId,
      };
      request({
        url: `/api/exam/TemsExamPaper/Actions/Export`,
        method: "GET",
        data: query,
      }).then((res) => {
        if (!res.data.url) return;
        this.jsbos.downloadFile(res.data.url);
        this.$refs.ExportBox.visible = false;
        this.exportBoxVisible = false;
      });
    },
    search() {
      this.initData();
    },
    refresh(isrRefresh, paperType) {
      this.formVisible = false;
      this.randomFormVisible = false;
      if (isrRefresh) this.reset();
    },
    resetListQuery() {
      this.listQuery = {
        keyword: "",
        currentPage: 1,
        pageSize: 20,
        sort: "asc",
        sidx: "",
      };
    },
    resetQuery() {
      for (let key in this.query) {
        this.query[key] = undefined;
      }
    },
    reset() {
      this.resetListQuery();
      this.resetQuery();
      this.search();
    },
  },
};
</script>
